package beanstao.util.store.database.impl;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import org.dom4j.Element;
import org.dom4j.Node;

import beanstao.util.log.LogHelper;

public class OracleVerify extends AbstractDbVerify
{
	@Override
	public boolean verify(Element element)
	{
		System.out.println("...ecms >>> 读取oralce数据库配置。开始验证数据库完整性。");
		Connection conn_0 = null;
		Statement st = null;
		boolean isconn = false;
		try
		{
			Class.forName("oracle.jdbc.driver.OracleDriver");
		}
		catch (ClassNotFoundException e)
		{
			System.err.println("数据库驱动未载入。" + e);
			return false;
		}

		String url = element.selectSingleNode("conns").getText().trim();
		for (Node node : (Element) element.selectSingleNode("main_guest_names"))
		{
			if (!(node instanceof Element))
				continue;
			Element subElement = (Element) node;
			String name = subElement.attributeValue("name");
			String value = subElement.attributeValue("value");
			try
			{
				conn_0 = DriverManager.getConnection(url, name, value);
				isconn = true;
				System.out.println(conn_0);
				System.out.println(">>>>oracle连接成功！！");
				break;
			}
			catch (SQLException e)
			{
				continue;
			}
		}
		if (!isconn)
		{
			System.err.println("OracleVerify校验时数据库无法连接。");
			return false;
		}
		String ecmsnameString = "";
		String ecmspwString = "";
		try
		{
			st = conn_0.createStatement();
			ecmsnameString = element.selectSingleNode("username").getText();
			ecmspwString = element.selectSingleNode("credentials").getText();

			ResultSet set = st.executeQuery("select username from dba_users where username='" + ecmsnameString.toUpperCase() + "'");

			boolean s = set.next();
			if (!s)
			{
				st.execute("create user " + ecmsnameString + " identified by " + ecmspwString);
				st.executeQuery("grant unlimited tablespace,Create sequence,create any view,drop any view,exp_full_database,imp_full_database,dba,connect,resource,create session to " + ecmsnameString);
				conn_0.commit();
			}
		}
		catch (SQLException e1)
		{
			System.err.println("数据库用户创建失败。");
			return false;
		}

		Element dbsElement = (Element) element.selectSingleNode("dbs");
		List<? extends Node> dbs = dbsElement.selectNodes("database");
		ArrayList<String> tablespaces = new ArrayList<String>();
		ArrayList<String> tablespacepath = new ArrayList<String>();
		for (Node node : dbs)
			if (node instanceof Element)
			{
				Element ss = (Element) node;
				String tp = ss.attributeValue("name");
				String path = ss.attributeValue("path");
				tablespaces.add(tp);
				tablespacepath.add(path);
			}
		Connection conn_1 = null;
		for (int i = 0; i < tablespaces.size(); i++)
		{
			// 创建表空间
			String tp = tablespaces.get(i);
			String path = tablespacepath.get(i);
			try
			{
				try
				{
					st = conn_0.createStatement();
					int r = st.executeUpdate("select name from v$tablespace where name='" + tp.toUpperCase() + "'");
					if (r == 0)
					{
						conn_1 = DriverManager.getConnection(url, ecmsnameString, ecmspwString);
						// st.execute("create tablespace " + db + " logging datafile" + "'" + db + ".dbf'" +
						// "size 5M extent management local AUTOALLOCATE");
						String size = element.selectSingleNode("size").getText();
						String resize = element.selectSingleNode("resize").getText();

						st = conn_1.createStatement();
						st.execute("create tablespace " + tp + " logging datafile '" + path + tp + ".dbf' size " + size + "m autoextend on next " + resize + "m maxsize unlimited extent management local autoallocate");
						System.out.println("...ecms >>> OracleVerify" + "创建表空间" + tp + "成功...");
					}
					else
						System.out.println("...ecms >>> 验证" + tp + "表空间正常。");
				}
				catch (Exception e)
				{
					e.printStackTrace();
					System.out.println("...ecms >>> OracleVerify 创建" + tp + "表空间异常。");
				}
			}
			catch (Exception e)
			{
				try
				{
					conn_1.close();
					conn_0.close();
					st.close();
				}
				catch (SQLException e1)
				{
					LogHelper.logInfo(e1, "OracleVerify" + "无法关闭连接对象，可能对系统产生影响。");
				}
			}
		}
		System.out.println("...ecms >>> 验证数据库表空间完整性结束。");
		return true;
	}

}
